﻿/*
 * ============ [ 废土 ] ============
 *
 * [ 维护者 ]  argoran@gmail.com
 * [  状态  ]  原型
 *
 * ============ [ 注释 ] ============
 *
 * 处理11号消息的类
 *
 * ==================================
 */

using System;

using LastLand.Data.Global;

using Up2Link.SegString;
using Up2Link.Server;

namespace LastLand.Core.MsgProcessor
{
    public class CMsgCreateNormal : IMessageProcessor
    {
        public void ProcessMessage(CMessagePack<CMessageSS> _msgPack)
        {
            Int32 index = _msgPack.index;
            String username = CData.connections[index].Account;

            if (CData.connections[index].IsLoggedIn()
                && !CData.connections[index].IsInGame()
                && CData.world.IsCharacterDead(CData.userAccounts[username].ActiveCharacterIndex))
            {
                Int32 characterIndex = CData.world.CreateNormalCharacter();
                if (characterIndex != -1)
                {
                    CData.userAccounts[username].CreateCharacter(characterIndex);
                    CMessageSS okCreateMsg = new CMessageSS(CConst.SERVER_MESSAGE_COMPLETE_CREATE_NORMAL, 0, CMessageSS.ENCODING_UTF32, "");
                    CMessagePack<CMessageSS> okCreateMsgPack = new CMessagePack<CMessageSS>(okCreateMsg, index);
                    CData.server.Send(okCreateMsgPack);
                }
                else
                {
                    CMessageSS noAvailableMsg = new CMessageSS(CConst.SERVER_MESSAGE_NO_COUNTRY_AVAILABLE, 0, CMessageSS.ENCODING_UTF32, "");
                    CMessagePack<CMessageSS> noAvailableMsgPack = new CMessagePack<CMessageSS>(noAvailableMsg, index);
                    CData.server.Send(noAvailableMsgPack);
                }
            }
            else if (!CData.connections[index].IsLoggedIn())
            {
                CMessageSS notLoggedInMsg = new CMessageSS(CConst.SERVER_MESSAGE_CLIENT_NOT_LOGGED_IN, 0, CMessageSS.ENCODING_UTF32, "");
                CMessagePack<CMessageSS> notLoggedInMsgPack = new CMessagePack<CMessageSS>(notLoggedInMsg, index);
                CData.server.Send(notLoggedInMsgPack);
            }
            else if (!CData.world.IsCharacterDead(CData.userAccounts[username].ActiveCharacterIndex))
            {
                CMessageSS notDeadMsg = new CMessageSS(CConst.SERVER_MESSAGE_CHARACTER_NOT_DEAD, 0, CMessageSS.ENCODING_UTF32, "");
                CMessagePack<CMessageSS> notDeadMsgPack = new CMessagePack<CMessageSS>(notDeadMsg, index);
                CData.server.Send(notDeadMsgPack);
            }
            else if (CData.connections[index].IsInGame())
            {
                CMessageSS inGameMsg = new CMessageSS(CConst.SERVER_MESSAGE_CLIENT_PLAYING, 0, CMessageSS.ENCODING_UTF32, "");
                CMessagePack<CMessageSS> inGameMsgPack = new CMessagePack<CMessageSS>(inGameMsg, index);
                CData.server.Send(inGameMsgPack);
            }
        }
    }
}
